import { isAuth } from '@/utils'
import { Redirect, Route } from 'react-router-dom'

// AuthRoute 用法：
// <AuthRoute path="/home" component={Home} />
function Auth({ component: Component, ...rest }) {
  // console.log('接收剩余参数：', rest)
  return (
    // 基本写法
    // <Route {...rest} render={() => <Component />} />
    // 鉴权写法
    <Route
      {...rest}
      render={(props) => {
        // console.log(2, props)
        if (!isAuth()) {
          return (
            <Redirect
              to={{
                pathname: '/login',
                // 跳转携带参数=》回跳地址from
                // state可以使任意类型值
                state: { from: props.location.pathname },
              }}
            />
          )
        }
        return <Component />
      }}
    />
  )
}

export default Auth
